<template>
	<div>
		<el-card class="box-card">
			<div class="Serach_b">
				<el-button type="primary" @click="AddClick()">添加</el-button>
				<el-input v-model="fy.roleName" type="input" autosize placeholder="请输入角色名称"
					@keyup.enter.native="SearchData" style="width: 300px;margin-left: 20px;" />
				<el-button type="primary" icon="el-icon-search" @click="SearchData()">搜索</el-button>
			</div>
			<el-table :data="RoleManageList" border>
				<el-table-column prop="roleKey" label="唯一角色编码" />
				<el-table-column prop="roleName" label="角色名称" />
				<el-table-column label="是否启用">
					<template slot-scope="scope">
						<span v-if="scope.row.enabled == 1">启用</span>
						<span v-else>禁用</span>
					</template>
				</el-table-column>
				<el-table-column prop="createBy" label="创建人" />
				<!-- <el-table-column prop="createTime" label="创建时间" /> -->
				<el-table-column prop="updateBy" label="修改人" />
				<!-- <el-table-column prop="updateTime" label="修改时间" /> -->
				<el-table-column label="操作" width="250px">
					<template slot-scope="scope">
						<div class="button">
							<el-button type="primary" size="mini" @click="UpdateClick(scope.row)">修改</el-button>
							<el-button type="danger" size="mini" @click="DelClick(scope.row)">删除</el-button>
							<el-button type="warning" icon="el-icon-s-tools" size="mini"
								@click="JurisdicClick(scope.row)">分配权限</el-button>
						</div>
					</template>
				</el-table-column>
			</el-table>
			<el-dialog :title="title" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
				<el-form label-position="left" label-width="80px" :model="ObjectList">
					<el-form-item label="角色名称">
						<el-input v-model="ObjectList.roleKey" placeholder="请输入角色名称" />
					</el-form-item>
					<el-form-item label="角色描述">
						<el-input v-model="ObjectList.roleName" placeholder="请输入角色描述" />
					</el-form-item>
					<el-form-item v-if="this.ObjectList.id" label="是否启用">
						<el-select v-model="ObjectList.enabled" placeholder="请选择">
							<el-option v-for="item in ShowList" :key="item.value" :label="item.label"
								:value="item.value" />
						</el-select>
					</el-form-item>
				</el-form>
				<span slot="footer" class="dialog-footer">
					<el-button @click="dialogVisible = false">取 消</el-button>
					<el-button type="primary" @click="ClickUser()">确 定</el-button>
				</span>
			</el-dialog>
			<el-dialog title="提示" :visible.sync="dialogVisible2" width="30%" :before-close="handleClose">
				<el-tree ref="treeRef" :data="QxMenuList" show-checkbox node-key="menuKey"
					:default-checked-keys="checkList" :props="defaultProps" />
				<span slot="footer" class="dialog-footer">
					<el-button @click="dialogVisible2 = false">取 消</el-button>
					<el-button type="primary" @click="UpdateQx()">修改</el-button>
				</span>
			</el-dialog>
			<el-pagination :page-sizes="[10, 18]" :page-size="fy.size" layout="total, sizes, prev, pager, next, jumper"
				:total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
		</el-card>
	</div>
</template>

<script>
	import * as QxRoleMenuApi from "@/api/PublicSearch";
	import * as syRoleManageApi from "@/api/Add";
	import * as RoleManageApi from "@/api/Search";
	import {
		timestampToTime
	} from "@/utils/date";
	export default {
		data() {
			return {
				RoleManageList: [],
				timestampToTime,
				title: "",
				dialogVisible: false,
				dialogVisible2: false,
				ObjectList: {},
				QxMenuList: [],
				fy: {
					size: 10,
					current: 1,
				},
				total: 0,
				ShowList: [{
						label: "启用",
						value: 1,
					},
					{
						label: "禁用",
						value: 0,
					},
				],
				defaultProps: {
					label: "menuName",
				},
				checkList: [],
				roleKey: "",
			};
		},
		created() {
			this.SearchData();
		},
		methods: {
			UpdateQx() {
				const keys = [
					// 将半选中和选中的从数组切割成字符串
					...this.$refs.treeRef.getCheckedKeys(),
					// ...this.$refs.treeRef.getHalfCheckedKeys(),
				].join(",");
				QxRoleMenuApi.UpdateMenu({
					roleKey: this.roleKey,
					menuKeys: keys,
					manageUserId: sessionStorage.getItem("userId"),
				}).then((res) => {
					if (res.success == true) {
						this.dialogVisible2 = false;
						this.SearchData();
						this.$notify({
							title: "成功",
							message: "修改成功",
							type: "success",
						});
						sessionStorage.setItem("roleMenuList", JSON.stringify(res.data));
					} else {
						this.dialogVisible2 = false;
						this.SearchData();
						this.$notify({
							title: "失败",
							message: "修改失败",
							type: "error",
						});
					}
				});
			},
			JurisdicClick(row) {
				this.checkList = [];
				this.roleKey = row.roleKey;
				QxRoleMenuApi.SsearchMenu({
					roleKey: row.roleKey
				}).then((res) => {
					this.dialogVisible2 = true;
					this.QxMenuList = res.data;
					const obj = {};
					for (let i = 0; i < this.QxMenuList.length; i++) {
						if (this.QxMenuList[i].flag == true) {
							this.checkList.push(this.QxMenuList[i].menuKey);
						}
					}
				});
			},
			AddClick() {
				this.title = "新增角色信息";
				this.dialogVisible = true;
				this.ObjectList = {};
			},
			ClickUser() {
				if (this.ObjectList.id) {
					syRoleManageApi.UpdateRoleMAnage(this.ObjectList).then((res) => {
						if (res.data == true) {
							this.dialogVisible = false;
							this.SearchData();
							this.$notify({
								title: "成功",
								message: "修改成功",
								type: "success",
							});
						} else {
							this.dialogVisible = false;
							this.SearchData();
							this.$notify({
								title: "失败",
								message: "修改失败",
								type: "error",
							});
						}
					});
				} else {
					syRoleManageApi.AddRoleMAnage(this.ObjectList).then((res) => {
						if (res.data == true) {
							this.dialogVisible = false;
							this.SearchData();
							this.$notify({
								title: "成功",
								message: "新增成功",
								type: "success",
							});
						} else {
							this.dialogVisible = false;
							this.SearchData();
							this.$notify({
								title: "失败",
								message: "新增失败",
								type: "error",
							});
						}
					});
				}
			},
			UpdateClick(row) {
				syRoleManageApi.UpdateRoleMAnageID({
					id: row.id
				}).then((res) => {
					this.ObjectList = res.data;
					this.dialogVisible = true;
				});
				this.title = "修改角色信息";
			},
			DelClick(row) {
				this.$confirm("确认删除？")
					.then((_) => {
						syRoleManageApi.DelRoleMAnage({
							idList: row.id
						}).then((res) => {
							if (res.data == true) {
								this.dialogVisible = false;
								this.SearchData();
								this.$notify({
									title: "成功",
									message: "删除成功",
									type: "success",
								});
							} else {
								this.dialogVisible = false;
								this.SearchData();
								this.$notify({
									title: "失败",
									message: "删除失败",
									type: "error",
								});
							}
						});
						done();
					})
					.catch((_) => {});
			},
			SearchData() {
				RoleManageApi.InsertSysRoleManage(this.fy).then((res) => {
					this.total = res.data.total;
					this.RoleManageList = res.data.records;
					this.RoleManageList.forEach((item) => {
						item.createTime = this.timestampToTime(item.createTime);
						item.updateTime = this.timestampToTime(item.updateTime);
					});
				});
			},
			handleSizeChange(value) {
				this.fy.size = value;
				this.SearchData();
			},
			handleCurrentChange(value) {
				this.fy.current = value;
				this.SearchData();
			},
			handleClose(done) {
				this.$confirm("确认关闭？")
					.then((_) => {
						done();
					})
					.catch((_) => {});
			},
		},
	};
</script>

<style lang="scss" scoped>
	.el-pagination {
		margin-top: 10px;
	}

	.box-card {
		width: 95%;
		margin: auto;
	}

	.Serach_b {
		display: flex;
		align-items: center;
		margin-bottom: 20px;
	}

	.Serach_b .el-button {
		margin-left: 20px;
	}

	.el-textarea {
		margin-left: 20px;
		width: 30%;
	}

	.button {
		display: flex;
	}
</style>
